<template>
  <div class="login">
    <div class="loginbox">
      <div class="img">
        <img src="@/assets/common/login-logo.png" alt="">
      </div>
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" prefix-icon="el-icon-user-solid" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            prefix-icon="el-icon-lock"
            show-password
          />
        </el-form-item>
        <el-form-item>
          <el-button :loading="loading" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 登录加载状态
      loading: false,
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      // 验证规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' },
          // 自定义验证手机号
          {
            validator: (rules, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              // 正则验证
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号!'))
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入6~12位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login() {
      // 获取全局表单验证,result返回boolean值
      this.$refs.form.validate(async(result) => {
        // 验证成功
        if (result) {
          this.loading = true
          // 请求失败的情况下,loading状态不会结束,会一直执行,所以需要try,catch来捕获错误,失败就关闭加载状态
          try {
            // 发起请求,验证用户名密码
            // 由于请求是异步方法,需要使用async await 关键字变成同步方法
            await this.$store.dispatch('user/toLogin', this.form)
            // 验证成功结束加载状态
            this.loading = false
            const redirect = this.$route.query.redirect || '/'
            // 验证成功跳转至登录页面
            this.$router.push(redirect)
            // console.log('验证成功!')
          } catch {
            // console.log(error)
            // 结束加载状态
            this.loading = false
          }
        } else {
          console.log('验证失败!')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .loginbox {
    width: 580px;
    // height: 500px;
    // background-color: #fff;
    .img {
      text-align: center;
      margin-bottom: 20px;
    }
    ::v-deep input.el-input__inner {
      padding-left: 50px;
      background-color: #d3e4ff;
      height: 50px;
      color: #7ec5ff;
    }
    ::v-deep .el-input__icon {
      color: #889aa4;
      font-size: 20px;
    }
    .el-button {
      width: 100%;
      height: 60px;
      background-color: #407ffe;
      color: #fff;
      border: 0;
      font-size: 20px;
    }
  }
}
</style>
